package com.shop.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.entity.pojo.*;
import com.shop.user.mapper.*;
import com.shop.user.service.StyleService;
import com.shop.utils.common.enums.Code;
import com.shop.utils.common.enums.Message;
import com.shop.utils.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class StyleServiceImpl extends ServiceImpl<StyleMapper,Style> implements StyleService {

    @Autowired
    private CostMapper costMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private StyleMapper styleMapper;
    @Override
    public Result<List<Style>> waresDetail(Long waresId){
        log.info("用户-商品款式");
        long start = System.currentTimeMillis();
        String key=Message.WeChat_Key_Style+waresId;
        List<Style> check = (List<Style>) redisTemplate.opsForValue().get(key);
        if (check != null && check.size() > Message.Check_Int_Null) {
            log.info("浏览缓存");
            long end = System.currentTimeMillis();
            log.info("用户在缓存中获取某个商品款式进程完成，总耗时:{}",(end-start));
            return Result.okResult(check);
        }
        log.info("浏览数据库");
        List<Style> styles = styleMapper.selectList(Wrappers.<Style>lambdaQuery().eq(Style::getWaresId, waresId).eq(Style::getState,Message.Put));
        if (CollectionUtils.isEmpty(styles)||styles.size()==Message.Check_Int_Null) {
            return Result.errorResult(Code.DATA_NOT_EXIST);
        }
        redisTemplate.opsForValue().set(key,styles);
        long end = System.currentTimeMillis();
        log.info("用户在数据库中获取某个商品款式进程完成，总耗时:{}",(end-start));
        return Result.okResult(styles);
    }
}
